package sxe.core.hash;

import java.sql.SQLException;

import snobject.snBoolean;
import snobject.snObject;
import snobject.snObjectArgList;
import step.EvaluationFailure;
import step.FunctionWrapper;
import step.exceptions.ResourceUnavailableException;
import sxe.core.EvaluationFailureReasons;

public class put extends FunctionWrapper {

	/* Usage (SNAFU style):
	 * 
	 * put("key", snObject);
	 * 
	 * (non-Javadoc)
	 * @see step.FunctionWrapper#Call(snobject.snObjectArgList)
	 */
	
	@Override
	public snObject Call(snObjectArgList argv) throws EvaluationFailure,
			ClassCastException, ArrayIndexOutOfBoundsException, ResourceUnavailableException {
		
		if(argv.length()<2)
			throw new EvaluationFailure(super.expnode,
					EvaluationFailureReasons.BAD_PARAMETER_COUNT);
		
		String key = argv.popString().getString();
		snObject data = argv.popObject();

		System.out.println("Calling hash.put with key="+key);

		try{
			SQLClientInstance.getInstance().Put(data,key);
			return new snBoolean(true);
		}
		catch(SQLException sqe){
			sqe.printStackTrace();
			throw new ResourceUnavailableException("SQLException in put: " + sqe.getMessage());
		}

	}

}
